<template>
    <div>
      <div style="margin: 10px 0">
        <el-input style="width: 600px" placeholder="请输入部门名称" suffix-icon="el-icon-search" v-model="departmentName"></el-input>
        <el-button class="ml-5" type="primary" @click="search">搜索</el-button>
        <el-button type="primary" @click="handleAdd">增加部门<i class="el-icon-circle-plus-outline"></i></el-button>
      </div>
      <el-table :data="tableData" border stripe :header-cell-class-name="headerBg" @selection-change="handleSelectionChange">
        <el-table-column type="selection"></el-table-column>
        <el-table-column prop="id" label="序号"></el-table-column>
        <el-table-column prop="departmentName" label="部门名称"></el-table-column>
        <el-table-column prop="workId" label="电话"></el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="scope">
            <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
            <el-popconfirm
                class="ml-5"
                confirm-button-text='确定'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定删除吗？"
                @confirm="del(scope.row.id)"
            >
              <el-button type="danger" slot="reference">删除<i class="el-icon-delete"></i></el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="padding: 10px 0">
        <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </div>
      <el-dialog title="修改部门" :visible.sync="dialogFormVisible" width="30%" >
        <el-form label-width="80px" size="small">
          <el-form-item label="序号">
            <el-input v-model="form.id" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="部门名称">
            <el-input v-model="form.departmentName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="form.phone" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </div>
      </el-dialog>
      <el-dialog title="增加部门" :visible.sync="dialogFormVisible1" width="30%" >
        <el-form label-width="80px" size="small">
          <el-form-item label="部门名称">
            <el-input v-model="form.departmentName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="form.phone" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible1 = false">取 消</el-button>
          <el-button type="primary" @click="addsave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  export default {
    name: "UserView",
    data(){
      return {
        tableData: [],
        departmentName: "",
        total: 0,
        pageNum: 1,
        pageSize: 10,
        form: {},
        dialogFormVisible: false,
        dialogFormVisible1: false,
        multipleSelection: [],
        headerBg: 'headerBg'
      }
    },
    created() {
      // 请求分页查询数据
      this.load()
    },
    methods:{
      load() {
        this.request.get("/selectdepartment",{
          params:{
            pageNum: this.pageNum,
            pageSize: this.pageSize,
          }
        }).then(res => {
          console.log("===========")
          console.log(res)
          this.tableData = res.data.list
          this.total = res.data.total
        })
      },
      search (){
        if(this.departmentName === ' '){
          this.load
        }else{
          this.request.get('/selectdepartmentByName',{          
          params:{
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            name: this.departmentName
          }
        }).then(res => {
          this.tableData = res.data.list
          this.total = res.data.total
        })
        }
      },
      save (){
        this.request.post("/updatedepartment",this.form).then(res => {
          if (res.code === '200'){
            this.$message.success("修改成功")
            this.dialogFormVisible = false
            this.load()
          }else {
            this.$message.error("修改失败")
          }
        })
      },
      addsave (){
        this.request.post("/adddepartment",this.form).then(res => {
          if(res.code === '200'){
            this.$message.success("新增成功")
            this.dialogFormVisible1 = false
            this.load()
          }else{
            this.$message.success("新增失败")
          }
          console.log(res)
        })
      },
      handleAdd (){
        this.dialogFormVisible1 = true
        this.form = {}
      },
      handleEdit (row) {
        this.form = row
        this.dialogFormVisible = true;
      },
      del (id){
        this.request.post("/deletedepartment/"+id).then(res => {
          if (res.code === '200'){
            this.$message.success("删除成功")
            this.load()
          }else {
            this.$message.error("删除失败")
          }
        })
      },
      handleSelectionChange (val){
        console.log(val)
        this.multipleSelection = val
      },
      handleSizeChange(pageSize) {
        console.log(pageSize)
        this.pageSize = pageSize
        this.load()
      },
      handleCurrentChange(pageNum) {
        console.log(pageNum)
        this.pageNum = pageNum
        this.load()
      },
    }
  }
  </script>
  
  <style>
  .headerBg {
    background: #eee!important;
  }
  </style>